import requests
from lxml import etree


num = 1
while True:
    url = 'https://www.zongheng.com/api2/catefine/storeSearch'

    header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 '
                      'Safari/537.36 Edg/135.0.0.0',

    }
    payload = {
        "worksTypes": 0,
        "bookType": 1,
        "subWorksTypes": 0,
        "totalWord": 0,
        "serialStatus": 1,
        "vip": 9,
        "pageNum": num,
        "pageSize": 20,
        "categoryId": 0,
        "categoryPid": 0,
        "naodongFilter": 0
    }
    req = requests.post(url, headers=header, params=payload)
    for i in req.json()['result']['bookList']:
        urls = f"https://huayu.zongheng.com/showchapter/{i['bookId']}.html"
        req = requests.get(urls, headers=header)
        tree = etree.HTML(req.text)
        links = tree.xpath('//li[@class=" col-4"]/a/@href')
        titles = tree.xpath('//li[@class=" col-4"]/a/text()')
        h1 = tree.xpath('//h1/text()')
        print(h1)
        for link, title in zip(links, titles):
            print(f"章节: {title}")
            content = requests.get(link, headers=header)
            tree = etree.HTML(content.text)
            yue = tree.xpath('//div[@class="content"]/p/text()')
            print(yue)

        num += 1